<template>
  <div class="app-container">
    <el-row :gutter="20" style="height: 800px">
      <!--用户数据-->
      <el-card class="box-card" style="width: 1000px; float: left; height: 800px; margin-left: 10px">
        <div slot="header" class="clearfix">
          <span>贷款的学生</span>
          <el-button style="float: right; padding: 3px 0" @click="resetQuery" type="text">重置</el-button>
        </div>
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="130px">
          <el-form-item prop="phonenumber" label="请输入机号码搜索">
            <el-input
              v-model="queryParams.phonenumber"
              placeholder="请输入手机号码"
              clearable
              size="small"
              style="width: 210px"
              @keyup.enter.native="handleQuery"
            />
            <el-button style="margin-left: 20px" type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          </el-form-item>
        </el-form>

        <el-col :span="24" :xs="24" style="float: left">

          <div slot="header" class="clearfix">
            <span>贷款的学生</span>
            <el-button style="float: right; padding: 3px 0" @click="resetQuery" type="text">重置</el-button>
          </div>
          <el-table @row-click="rowClick" :cell-style="{padding:'10px'}" v-loading="loading" :data="userList">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="学生姓名" prop="userName" width="120" align="center" />
            <el-table-column label="学生昵称" align="center" prop="nickName" />
            <el-table-column label="手机号码" align="center" prop="phonenumber" width="180" />
            <el-table-column label="创建时间" align="center" prop="createTime" width="200">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime) }}</span>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-col>
      </el-card>

      <el-card class="box-card" style="width: 1000px; float: left; margin-top: 10px; margin-left: 10px">
        <div slot="header" class="clearfix">
          <span>进行审批</span>
          <el-button style="float: right; padding: 3px 0" @click="resetQuery" type="text">重置</el-button>
        </div>
        <el-table v-loading="loading" :data="checkList">
          <el-table-column width="120" label="年度" align="center" prop="duringYear" fixed="left" />
          <el-table-column label="借款人信息(学生)" align="center" >
            <el-table-column width="120" label="QQ" align="center" prop="qq" />
            <el-table-column width="180" label="学生身份证" align="center" prop="cardIdentity" />
            <el-table-column width="120" label="户口性质" align="center" prop="residence" />
            <el-table-column width="120" label="毕业中学" align="center" prop="medium" :show-overflow-tooltip="true" />
            <el-table-column width="120" label="户籍" align="center" prop="household" :show-overflow-tooltip="true" />
            <el-table-column width="120" label="申贷原因" align="center" prop="applyReason" :show-overflow-tooltip="true" />
            <el-table-column width="180" label="学生身份证有效起始日期" align="center" prop="studentEffectiveCardIdentity">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.studentEffectiveCardIdentity) }}</span>
              </template>
            </el-table-column>
            <el-table-column width="180" label="学生身份证有效结束日期" align="center" prop="studentInvalidCardIdentity" >
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.studentInvalidCardIdentity) }}</span>
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column width="120" label="共同借款人" align="center" :show-overflow-tooltip="true" >
            <el-table-column width="120" label="共同借款人姓名" align="center" prop="guarantor" :show-overflow-tooltip="true" />
            <el-table-column width="180" label="共同借款人联系电话" align="center" prop="guaPhone" :show-overflow-tooltip="true" />
            <el-table-column width="120" label="关系" align="center" prop="relation" :show-overflow-tooltip="true" />
            <el-table-column width="180" label="共同借款人身份证号" align="center" prop="guaCardIdentity" :show-overflow-tooltip="true" />
            <el-table-column width="120" label="健康状态" align="center" prop="heath" :show-overflow-tooltip="true" />
          </el-table-column>
          <el-table-column width="240" label="资助中心" align="center" >
            <template slot-scope="scope">
              <span>{{ scope.row.supportCenterProvince }} {{ scope.row.supportCenterCity }} {{ scope.row.supportCenterArea }}</span>
            </template>
          </el-table-column>
          <el-table-column width="240" label="入学前户籍所在地" align="center" >
            <template slot-scope="scope">
              <span>{{ scope.row.householdOfProvince }} {{ scope.row.householdOfCity }} {{ scope.row.householdOfArea }} {{ scope.row.householdOfCounty }} {{ scope.row.householdOfDetail }}</span>
            </template>
          </el-table-column>
          <el-table-column label="家庭住址" align="center" :show-overflow-tooltip="true" >
            <el-table-column width="240" label="家庭住址所在地" align="center" >
              <template slot-scope="scope">
                <span>{{ scope.row.homeOfProvince }} {{ scope.row.homeOfCity }} {{ scope.row.homeOfArea }} {{ scope.row.homeOfCounty }} {{ scope.row.homeOfDetail }}</span>
              </template>
            </el-table-column>
            <el-table-column width="140" label="是否与户籍地址一致" align="center" prop="isTheSameAsHousehold" :show-overflow-tooltip="true" >
              <template slot-scope="scope">
                <span>{{ scope.row.isTheSameAsHousehold === 'true' ? '是' : '否'  }}</span>
              </template>
            </el-table-column>
            <el-table-column width="140" label="家庭住址邮政编码" align="center" prop="homeOfPostcode" :show-overflow-tooltip="true" />
            <el-table-column width="120" label="家庭电话" align="center" prop="homeOfPhone" :show-overflow-tooltip="true" />
          </el-table-column>
          <el-table-column label="就学信息" align="center" :show-overflow-tooltip="true" >
            <el-table-column label="高校名称" align="center" prop="collegeOfName" :show-overflow-tooltip="true" />
            <el-table-column width="160" label="院系名称" align="center" prop="collegeOfInstitute" :show-overflow-tooltip="true" />
            <el-table-column label="专业名称" align="center" prop="collegeOfMajor" :show-overflow-tooltip="true" />
            <el-table-column label="入学时间" align="center" prop="collegeWithStart" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.collegeWithStart) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="学历" align="center" prop="collegeOfEducation" :show-overflow-tooltip="true" />
            <el-table-column width="120" label="学生学号" align="center" prop="studentNo" :show-overflow-tooltip="true" />
            <el-table-column label="专业类别" align="center" prop="typeOfMajor" :show-overflow-tooltip="true" />
            <el-table-column label="学制" align="center" prop="collegeOfEducationalSystem" :show-overflow-tooltip="true" />
            <el-table-column label="毕业时间" align="center" prop="collegeWithEnd" width="120" />
          </el-table-column>
          <el-table-column width="120" label="资格审查确认" align="center" :show-overflow-tooltip="true" >
            <el-table-column width="120" label="资格审查单位" align="center" prop="countyUnit" :show-overflow-tooltip="true" />
            <el-table-column width="120" label="是否盖章" align="center" prop="isSeal" :show-overflow-tooltip="true" />
            <el-table-column width="120" label="盖章日期" align="center" prop="sealDate" :show-overflow-tooltip="true" />
            <el-table-column width="120" label="学生签字日期" align="center" prop="signatureDate"/>
          </el-table-column>
          <el-table-column label="就业信息" align="center" :show-overflow-tooltip="true" >
            <el-table-column label="工作单位" align="center" prop="workFeature" :show-overflow-tooltip="true" />
            <el-table-column label="单位性质" align="center" prop="phoneOfUnit" :show-overflow-tooltip="true" />
            <el-table-column label="单位电话" align="center" prop="postCodeOfUnit" :show-overflow-tooltip="true" />
            <el-table-column width="140" label="单位邮政编码" align="center" prop="addressOfUnit" :show-overflow-tooltip="true" />
            <el-table-column label="单位地址" align="center" prop="addressOfUnit" :show-overflow-tooltip="true" />
          </el-table-column>
          <el-table-column label="变更" align="center" prop="changeForReason" :show-overflow-tooltip="true" />
          <el-table-column width="100" label="是否已签章" align="center" prop="isSeal" :show-overflow-tooltip="true" />
          <el-table-column label="双方签订合同状态" fixed="right" width="200" align="center" :show-overflow-tooltip="true" >
            <el-table-column label="借款人" width="100" align="center" prop="firstSignature" :show-overflow-tooltip="true" >
              <template slot-scope="scope">
                {{ scope.row.firstSignature ? '已签' : '未签' }}
              </template>
            </el-table-column>
            <el-table-column label="县资助中心" width="100" align="center" prop="secondSignature" :show-overflow-tooltip="true" >
              <template slot-scope="scope">
                {{ scope.row.secondSignature ? '已签' : '未签' }}
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column label="县资助中心审批状态" prop="creditStatus" align="center" fixed="right" width="140" class-name="small-padding fixed-width" />
          <el-table-column label="操作" align="center" fixed="right" width="140" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.isBankCheck === '已审批'"
                size="small"
                type="success"
                icon="el-icon-edit"
              >已审批</el-button>
              <el-button
                v-if="scope.row.isBankCheck !== '已审批'"
                size="small"
                type="primary"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >请审批</el-button>
            </template>
          </el-table-column>
        </el-table>

      </el-card>

    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名称" prop="nickName">
              {{form.userName}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户昵称" prop="nickName">
              {{form.nickName}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phonenumber">
              {{form.phonenumber}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              {{form.email}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item v-if="form.userId == undefined" label="用户名称" prop="userName">
              {{form.userName}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.userId == undefined" label="用户密码" prop="password">
              {{form.password}}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { getData, updateData, } from "@/api/county/credit";
  import { getToken } from "@/utils/auth";
  import { treeselect } from "@/api/system/dept";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import { selectUserListForStudent } from '../../../api/system/user'
  import { getCredits } from '../../../api/county/credit'

  export default {
    name: "User",
    components: { Treeselect },
    data() {
      return {
        currentName: '',
        checkList: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 用户表格数据
        userList: null,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 日期范围
        dateRange: [],
        // 状态数据字典
        // 表单参数
        form: {},
        // 用户导入参数
        upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/system/user/importData"
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 6,
          userName: undefined,
          phonenumber: undefined,
          status: undefined,
          deptId: undefined
        },
        // 表单校验
        rules: {
        }
      };
    },
    watch: {
      // 根据名称筛选部门树
      deptName(val) {
        this.$refs.tree.filter(val);
      }
    },
    created() {
      this.getList();
      this.getTreeselect();
    },
    methods: {
      rowClick(data) {
        // 暂时保存该字段，用于子表更新后父表更新
        this.currentName = data.userName
        getCredits(data.userName).then(response => {
          console.log(response)
          this.checkList = response.data
        })
      },
      /** 查询用户列表 */
      getList() {
        this.loading = true;
         selectUserListForStudent().then(response => {
          this.userList = response.userListForStudent;
          console.log(response)
          this.total = response.userListForStudent.length;
          this.loading = false;
        })
      },
      /** 查询部门下拉树结构 */
      getTreeselect() {
        treeselect().then(response => {
          this.deptOptions = response.data;
        });
      },
      // 筛选节点
      filterNode(value, data) {
        if (!value) return true;
        return data.label.indexOf(value) !== -1;
      },
      // 节点单击事件
      handleNodeClick(data) {
        this.queryParams.deptId = data.id;
        this.getList();
      },
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          userId: undefined,
          deptId: undefined,
          userName: undefined,
          nickName: undefined,
          phonenumber: undefined,
          email: undefined,
          sex: undefined,
          remark: undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        const creditId = row.creditId || this.ids
        getData(creditId).then(response => {
          this.form = response.data;
          this.form.isBankCheck = '已审批'
          this.$confirm('确认借款人 ' + row.userName + ' 的贷款审批通过吗? 是否确定继续操作?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            updateData(this.form).then(() => {
              this.msgSuccess('审批成功!')
              this.getList()
              getCredits(this.currentName).then(response => {
                console.log(response)
                this.checkList = response.data
              })
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消 ' + row.userName + ' 的贷款审批操作'
            });
          });
        });
      },
    }
  };
</script>
